package com.awesome.javademo.algorithm.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.awesome.javademo.R;
import com.awesome.javademo.algorithm.solution.SolutionLinkedList;
import com.awesome.javademo.algorithm.bean.ListNode;
import com.awesome.common.util.log.ShowLogUtil;

public class LinkedListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linked_list);

//        ListNode removeNthFromEndInit = getLinkedList1();
//        ListNode removeNthFromEnd = SolutionLinkedList.removeNthFromEnd(removeNthFromEndInit,3);
//        printLinkedList(removeNthFromEnd);

        ListNode reverseListInit = getLinkedList1();
        printLinkedList(reverseListInit);
        ListNode reverseList = SolutionLinkedList.reverseList(reverseListInit);
        printLinkedList(reverseList);


    }

    /**
     * 获取LinkedList
     * @return
     */
    private ListNode getLinkedList1(){
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        return listNode1;
    }

    /**
     * 打印链表
     * @param listNode
     */
    private void printLinkedList(ListNode listNode) {
        ShowLogUtil.info("==========start============");
        while (listNode != null) {
            ShowLogUtil.info("" + listNode.val);
            listNode = listNode.next;
        }
        ShowLogUtil.info("==========end============");
    }
}